package com.jzo2o.customer.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.Result;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 赵超
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址簿相关接口")
public class AddressController {

    private final IAddressBookService addressBookService;

    @GetMapping("/page")
    @ApiOperation("分页查询地址簿列表")
    public PageResult<AddressBook> pageList(AddressBookPageQueryReqDTO dto) {
        return addressBookService.pageList(dto);
    }

    @PostMapping
    @ApiOperation("新增地址簿")
    public Result<Object> add(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.add(dto);
        return Result.ok("新增成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询地址簿详情")
    public AddressBookResDTO getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtil.toBean(addressBook, AddressBookResDTO.class);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新地址簿")
    public Result<Object> updateById(
            @PathVariable Long id,
            @RequestBody AddressBookUpsertReqDTO dto
    ) {
        addressBookService.updateById(id, dto);
        return Result.ok("更新成功");
    }

    @DeleteMapping("/batch")
    @ApiOperation("删除地址簿")
    public Result<Object> deleteById(@RequestBody List<String> ids) {
        addressBookService.removeByIds(ids);
        return Result.ok("删除成功");
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void setDefault(@RequestParam Long id, @RequestParam Integer flag) {
        addressBookService.setDefault(flag, id);
    }
}
